/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.zakariya.stickyheadersapp.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.bundle.AbilityInfo;
import org.zakariya.stickyheaders.StickyHeaderLayoutManager;
import org.zakariya.stickyheaders.TabItemChangeListener;
import org.zakariya.stickyheadersapp.ResourceTable;
import org.zakariya.stickyheadersapp.adapter.TabPageAdapter;

import java.util.ArrayList;
import java.util.List;
/**
 * MainAbilitySlice
 *
 * @since 2021-08-20
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int DEFAULE_INDEX = 0;
    private static final int WIDTH_300 = 300;
    /**
     * tab页切换状态
     */
    private static TabItemChangeListener tabListener;
    private PageSlider mPageSlider;
    private TabList mTabList;
    private TabPageAdapter tabPageAdapter;
    private TabList.Tab defaultTab = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_view_pager);
        mTabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        mTabList.setTabLength(WIDTH_300); // 设置Tab的宽度
        TabList.Tab tab1 = mTabList.new Tab(getContext());
        tab1.setText(ResourceTable.String_tab_demos);
        mTabList.addTab(tab1);
        TabList.Tab tab2 = mTabList.new Tab(getContext());
        tab2.setText(ResourceTable.String_tab_sticky_about);
        mTabList.addTab(tab2);
        List<String> tabTitleList = new ArrayList<>();
        tabTitleList.add(tab1.getText());
        tabTitleList.add(tab2.getText());
        mPageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);
        /**
         * 绑定适配器
         */
        tabPageAdapter = new TabPageAdapter(this, tabTitleList);
        mPageSlider.setProvider(tabPageAdapter);
        addListener();
        mPageSlider.setCurrentPage(DEFAULE_INDEX);
        /**
         * 设置默认选择的页签项
         */
        mTabList.getTabAt(DEFAULE_INDEX).select();
    }

    private void addListener() {
        mPageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            /**
             * 切换page页过程
             *
             * @param i 表示正在显示目标页面
             * @param v 指示页面的位置偏移量。值的范围是(0,1]。
             * @param i1 指示显示页面的位置偏移像素数。
             */
            @Override
            public void onPageSliding(int i, float v, int i1) {
                /**
                 * 需要添加手势判断，只有当PageSlider处于按压状态时，TabList才可以跟随滑动
                 * 取PageSlider的二分之一效果为好  mTabList.fluentScrollXTo(i1 / 2);
                 */
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                mTabList.selectTab(mTabList.getTabAt(i));
            }
        });
        mTabList.addTabSelectedListener(new TabSelectListener(mPageSlider));
        if (tabListener == null) {
            tabListener = new StickyHeaderLayoutManager.MyTabListener();
        }
    }

    /**
     * tab选中状态接口
     *
     * @since 2021-04-12
     */
    public class TabSelectListener implements TabList.TabSelectedListener {
        private final PageSlider pageSlider;

        /**
         * page构造函数
         *
         * @param pageSlider 添加监听的控件
         */
        public TabSelectListener(PageSlider pageSlider) {
            this.pageSlider = pageSlider;
        }

        @Override
        public void onSelected(TabList.Tab tab) {
            if (tab == null) {
                return;
            }
            if (tabListener != null) {
                tabListener.onTabChangeStatus(true);
            }
            if (pageSlider != null) {
                pageSlider.setCurrentPage(tab.getPosition());
            }
            mTabList.fluentScrollXTo((int) tab.getContentPositionX());
            defaultTab = tab;
        }

        @Override
        public void onUnselected(TabList.Tab tab) {
            if (tabListener != null) {
                tabListener.onTabChangeStatus(false);
            }
        }

        @Override
        public void onReselected(TabList.Tab tab) {
        }
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
    }

}